home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / PIL / ImageMath.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  8.5 KB  |  264 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import Image
  5. import _imagingmath
  6. VERBOSE = 0
  7.  
  8. def _isconstant(v):
  9.     if not isinstance(v, type(0)):
  10.         pass
  11.     return isinstance(v, type(0))
  12.  
  13.  
  14. class _Operand:
  15.     
  16.     def __init__(self, im):
  17.         self.im = im
  18.  
  19.     
  20.     def _Operand__fixup(self, im1):
  21.         if isinstance(im1, _Operand):
  22.             if im1.im.mode in ('1', 'L'):
  23.                 return im1.im.convert('I')
  24.             elif im1.im.mode in ('I', 'F'):
  25.                 return im1.im
  26.             else:
  27.                 raise ValueError, 'unsupported mode: %s' % im1.im.mode
  28.         elif _isconstant(im1) and self.im.mode in ('1', 'L', 'I'):
  29.             return Image.new('I', self.im.size, im1)
  30.         else:
  31.             return Image.new('F', self.im.size, im1)
  32.  
  33.     
  34.     def apply(self, op, im1, im2 = None, mode = None):
  35.         im1 = self._Operand__fixup(im1)
  36.         if im2 is None:
  37.             if not mode:
  38.                 pass
  39.             out = Image.new(im1.mode, im1.size, None)
  40.             im1.load()
  41.             
  42.             try:
  43.                 op = getattr(_imagingmath, op + '_' + im1.mode)
  44.             except AttributeError:
  45.                 raise TypeError, "bad operand type for '%s'" % op
  46.  
  47.             _imagingmath.unop(op, out.im.id, im1.im.id)
  48.         else:
  49.             im2 = self._Operand__fixup(im2)
  50.             if im1.mode != im2.mode:
  51.                 if im1.mode != 'F':
  52.                     im1 = im1.convert('F')
  53.                 
  54.                 if im2.mode != 'F':
  55.                     im2 = im2.convert('F')
  56.                 
  57.                 if im1.mode != im2.mode:
  58.                     raise ValueError, 'mode mismatch'
  59.                 
  60.             
  61.             if im1.size != im2.size:
  62.                 size = (min(im1.size[0], im2.size[0]), min(im1.size[1], im2.size[1]))
  63.                 if im1.size != size:
  64.                     im1 = im1.crop((0, 0) + size)
  65.                 
  66.                 if im2.size != size:
  67.                     im2 = im2.crop((0, 0) + size)
  68.                 
  69.                 if not mode:
  70.                     pass
  71.                 out = Image.new(im1.mode, size, None)
  72.             elif not mode:
  73.                 pass
  74.             out = Image.new(im1.mode, im1.size, None)
  75.             im1.load()
  76.             im2.load()
  77.             
  78.             try:
  79.                 op = getattr(_imagingmath, op + '_' + im1.mode)
  80.             except AttributeError:
  81.                 raise TypeError, "bad operand type for '%s'" % op
  82.  
  83.             _imagingmath.binop(op, out.im.id, im1.im.id, im2.im.id)
  84.         return _Operand(out)
  85.  
  86.     
  87.     def __nonzero__(self):
  88.         return self.im.getbbox() is not None
  89.  
  90.     
  91.     def __abs__(self):
  92.         return self.apply('abs', self)
  93.  
  94.     
  95.     def __pos__(self):
  96.         return self
  97.  
  98.     
  99.     def __neg__(self):
  100.         return self.apply('neg', self)
  101.  
  102.     
  103.     def __add__(self, other):
  104.         return self.apply('add', self, other)
  105.  
  106.     
  107.     def __radd__(self, other):
  108.         return self.apply('add', other, self)
  109.  
  110.     
  111.     def __sub__(self, other):
  112.         return self.apply('sub', self, other)
  113.  
  114.     
  115.     def __rsub__(self, other):
  116.         return self.apply('sub', other, self)
  117.  
  118.     
  119.     def __mul__(self, other):
  120.         return self.apply('mul', self, other)
  121.  
  122.     
  123.     def __rmul__(self, other):
  124.         return self.apply('mul', other, self)
  125.  
  126.     
  127.     def __div__(self, other):
  128.         return self.apply('div', self, other)
  129.  
  130.     
  131.     def __rdiv__(self, other):
  132.         return self.apply('div', other, self)
  133.  
  134.     
  135.     def __mod__(self, other):
  136.         return self.apply('mod', self, other)
  137.  
  138.     
  139.     def __rmod__(self, other):
  140.         return self.apply('mod', other, self)
  141.  
  142.     
  143.     def __pow__(self, other):
  144.         return self.apply('pow', self, other)
  145.  
  146.     
  147.     def __rpow__(self, other):
  148.         return self.apply('pow', other, self)
  149.  
  150.     
  151.     def __invert__(self):
  152.         return self.apply('invert', self)
  153.  
  154.     
  155.     def __and__(self, other):
  156.         return self.apply('and', self, other)
  157.  
  158.     
  159.     def __rand__(self, other):
  160.         return self.apply('and', other, self)
  161.  
  162.     
  163.     def __or__(self, other):
  164.         return self.apply('or', self, other)
  165.  
  166.     
  167.     def __ror__(self, other):
  168.         return self.apply('or', other, self)
  169.  
  170.     
  171.     def __xor__(self, other):
  172.         return self.apply('xor', self, other)
  173.  
  174.     
  175.     def __rxor__(self, other):
  176.         return self.apply('xor', other, self)
  177.  
  178.     
  179.     def __lshift__(self, other):
  180.         return self.apply('lshift', self, other)
  181.  
  182.     
  183.     def __rshift__(self, other):
  184.         return self.apply('rshift', self, other)
  185.  
  186.     
  187.     def __eq__(self, other):
  188.         return self.apply('eq', self, other)
  189.  
  190.     
  191.     def __ne__(self, other):
  192.         return self.apply('ne', self, other)
  193.  
  194.     
  195.     def __lt__(self, other):
  196.         return self.apply('lt', self, other)
  197.  
  198.     
  199.     def __le__(self, other):
  200.         return self.apply('le', self, other)
  201.  
  202.     
  203.     def __gt__(self, other):
  204.         return self.apply('gt', self, other)
  205.  
  206.     
  207.     def __ge__(self, other):
  208.         return self.apply('ge', self, other)
  209.  
  210.  
  211.  
  212. def imagemath_int(self):
  213.     return _Operand(self.im.convert('I'))
  214.  
  215.  
  216. def imagemath_float(self):
  217.     return _Operand(self.im.convert('F'))
  218.  
  219.  
  220. def imagemath_equal(self, other):
  221.     return self.apply('eq', self, other, mode = 'I')
  222.  
  223.  
  224. def imagemath_notequal(self, other):
  225.     return self.apply('ne', self, other, mode = 'I')
  226.  
  227.  
  228. def imagemath_min(self, other):
  229.     return self.apply('min', self, other)
  230.  
  231.  
  232. def imagemath_max(self, other):
  233.     return self.apply('max', self, other)
  234.  
  235.  
  236. def imagemath_convert(self, mode):
  237.     return _Operand(self.im.convert(mode))
  238.  
  239. ops = { }
  240. for k, v in globals().items():
  241.     if k[:10] == 'imagemath_':
  242.         ops[k[10:]] = v
  243.         continue
  244.  
  245.  
  246. def eval(expression, _dict = { }, **kw):
  247.     args = ops.copy()
  248.     args.update(_dict)
  249.     args.update(kw)
  250.     for k, v in args.items():
  251.         if hasattr(v, 'im'):
  252.             args[k] = _Operand(v)
  253.             continue
  254.     
  255.     import __builtin__
  256.     out = __builtin__.eval(expression, args)
  257.     
  258.     try:
  259.         return out.im
  260.     except AttributeError:
  261.         return out
  262.  
  263.  
  264.